package com.etc.reflectdemo;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author: knowno
 * @create: 2024-09-18 14:14
 * @Version 1.0
 **/
public class TestMethod {
    public static void main(String[] args) throws NoSuchFieldException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Class<Person> personClass = Person.class;

        //构造一个person对象
        Person person = personClass.newInstance();

        Method method1 = personClass.getDeclaredMethod("method1");


        //如何调用这个方法
        //Object invoke(Object obj, Object... args)
        //在具有指定参数的 方法对象上调用此 方法对象表示的底层方法。
        method1.invoke(person,null);


        //私有的方法
        Method method2 = personClass.getDeclaredMethod("method2");
        method2.setAccessible(true);
        method2.invoke(person,null);


    }
}
